/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal class ReactiveDataPathEnumerator : ReactiveEnumerator<DataPath>
  {
    internal ReactiveDataPathEnumerator(
        GroupEnumerator groupEnumerator,
        DataItemFactory<DataPath> dataItemFactory,
        ThrottlingHandler throttlingHandler,
        GroupHelper groupHelper )
      : base( groupEnumerator, dataItemFactory, throttlingHandler )
    {
      m_groupHelper = groupHelper;
    }

    internal Func<DataPath, bool> ShouldPerformFetchDelegate
    {
      get;
      set;
    }

    public void SetItemTemplateOverride( DataPath itemTemplate )
    {
      //A "null" itemTemplate is a valid "override". That
      //is why there is a "m_useItemTemplateOverride" bool flag
      //to hold the "validity" of the m_itemTemplateOverride value.
      m_itemTemplateOverride = itemTemplate;
      m_useItemTemplateOverride = true;
    }

    public void ClearItemTemplateOverride()
    {
      m_itemTemplateOverride = null;
      m_useItemTemplateOverride = false;
    }

    protected override DataPath CreateDummyItem( DataPath templateItem )
    {
      if( m_useItemTemplateOverride )
      {
        return base.CreateDummyItem( m_itemTemplateOverride );
      }
      else
      {
        return base.CreateDummyItem( templateItem );
      }
    }

    protected override bool ShouldPerformFetch( DataPath item )
    {
      // If the dummy for the item that is about to be fetched is no longer in the list
      // of dummies of the cache, it means that the item is no longer needed.
      return ( this.ShouldPerformFetchDelegate != null )
        ? this.ShouldPerformFetchDelegate( item )
        : base.ShouldPerformFetch( item );
    }

    protected override bool DummyDependOn( DataPath doesThisDummy, DataPath dependOnThisOne )
    {
      return ( doesThisDummy.IsAncestorOf( dependOnThisOne )
        || base.DummyDependOn( doesThisDummy, dependOnThisOne ) );
    }

    protected override DataPath CreateDummyTemplate( SourcePosition sourcePosition )
    {
      DataPath datapath = base.CreateDummyTemplate( sourcePosition );

      if( datapath == null )
      {
        if( sourcePosition is SortPosition )
        {
          Nullable<StartPosition> startPosition;
          SortPosition source = ( SortPosition )sourcePosition;
          m_groupHelper.CreatePathFromSortPosition( source, out datapath, out startPosition );

          datapath = datapath.ParentPath;
        }
      }
      return datapath;
    }

    private DataPath m_itemTemplateOverride;
    private bool m_useItemTemplateOverride;
    private GroupHelper m_groupHelper;
  }
}
